var t = require;
var i = module;
var o,
    s = (o = t("astar")) && o.__esModule ? o : {default: o};
cc.Class({
    extends: cc.Component,
    properties: {mask: cc.Node, label: cc.Label, tex: cc.Sprite, nodeAni: cc.Node},
    start: function start() {
        this.node.on("onCollisionEnter", this.onCollisionEnter.bind(this)),
            this.node.on("onCollisionStay", this.onCollisionStay.bind(this)),
            this.node.on("onCollisionExit", this.onCollisionExit.bind(this)),
            (this.defSpd = 20),
            (this.spdX = 0),
            (this.spdY = 0),
            (this.dirX = 0),
            (this.dirY = 0),
            (this.score = 1),
            (this.triggerTimeMax = 2),
            (this.triggerTime = 0),
            (this.trackReLocationTimeMax = 0.3),
            (this.trackReLocationTime = this.trackReLocationTimeMax),
            (this.trackTag = null),
            (this.trackRoadPoints = null),
            (this.trackRoadPointIndex = 0),
            (this.trackTriggerPos = null);
    },
    setType: function setType(t) {
        var i = t.split("_"),
            o = i[0],
            s = null;
        i.length > 1 && ((o = i[0]), (s = cc.v2(Number(i[1]), Number(i[2]))), i[3] && (this.isTrigger = i[3])),
            (this.word = o),
            (this.triggerPos = s),
            (this.label.string = this.word);
    },
    startMove: function startMove(t, i) {
        0 == this.spdX &&
            0 == this.spdY &&
            ((this.dirX = t), (this.dirY = i), (this.spdX = this.defSpd * t), (this.spdY = this.defSpd * i));
    },
    getNextLogicPos: function getNextLogicPos() {
        var t = window.packMan.getLogicPosByPixelPos(this.node.position);
        return (t.x += this.dirX), (t.y -= this.dirY), t;
    },
    getNextLogicObj: function getNextLogicObj(t) {
        return (t = t || this.getNextLogicPos()), window.packMan.getWordByLogicPos(t.x, t.y);
    },
    getMoveMaxMinPos: function getMoveMaxMinPos(t) {
        var i = this.getNextLogicPos();
        t || (t = this.getNextLogicObj(i));
        var o = null;
        if (t)
            if ("金" == t.word || "银" == t.word);
            else {
                var s = window.packMan.getPixelPosByLogicPos(i.x, i.y),
                    e = s.x,
                    a = s.y;
                "门" != t.word
                    ? ((e -= this.dirX * window.packMan.wordWidth), (a -= this.dirY * window.packMan.wordHeight))
                    : ((e = e), (a = a)),
                    (o = cc.v2(e, a));
            }
        return o;
    },
    update: function update(t) {
        if (this.trackTag && this.trackRoadPoints) {
            var i = this.trackRoadPoints[this.trackRoadPointIndex],
                o = window.packMan.getPixelPosByLogicPos(i.y, i.x),
                s = cc.v2(o.x, o.y),
                e = s.x - this.node.x,
                a = 0;
            0 != e && (a = e / Math.abs(e)), (this.node.x += this.defSpd * a * 0.1);
            var n = s.y - this.node.y,
                r = 0;
            0 != n && (r = n / Math.abs(n)),
                (this.node.y += this.defSpd * r * 0.1),
                this.node.position.sub(s).mag() <= this.defSpd &&
                    ((this.node.position = s),
                    this.trackRoadPointIndex++,
                    this.trackRoadPointIndex >= this.trackRoadPoints.length && (this.trackRoadPoints = null));
        } else if (0 != this.spdX || 0 != this.spdY) {
            var h = this.getNextLogicPos(),
                c = this.getNextLogicObj(h),
                d = this.getMoveMaxMinPos(c);
            this.mmpos || (c && d)
                ? (this.mmpos || ((this.mmpos = d), (this.pcw = c)),
                  (this.node.x += this.spdX),
                  ((1 == this.dirX && this.node.x >= this.mmpos.x) ||
                      (-1 == this.dirX && this.node.x <= this.mmpos.x)) &&
                      ((this.node.x = this.mmpos.x), (this.spdX = 0), this.pcw.beTouched(), (this.mmpos = null)),
                  (this.node.y += this.spdY),
                  ((1 == this.dirY && this.node.y >= this.mmpos.y) ||
                      (-1 == this.dirY && this.node.y <= this.mmpos.y)) &&
                      ((this.node.y = this.mmpos.y), (this.spdY = 0), this.pcw.beTouched(), (this.mmpos = null)))
                : ((this.node.x += this.spdX), (this.node.y += this.spdY));
        }
        this.triggerTime > 0 && (this.triggerTime -= t),
            this.trackTag &&
                (this.trackReLocationTime > 0
                    ? (this.trackReLocationTime -= t)
                    : ((this.trackReLocationTime = this.trackReLocationTimeMax),
                      this.updateTrackPos(),
                      this.updateAni()));
    },
    beTouched: function beTouched() {
        "门" == this.word && window.ui.win({model: "PackMan", lv: window.packMan.level});
    },
    onCollisionEnter: function onCollisionEnter(t) {
        if ("我" == this.word) {
            var i = t.getUserData(),
                o = i.other,
                s = (i.self, o.node.parent.parent.getComponent("PMWordBase"));
            s &&
                ("金" == s.word || "银" == s.word
                    ? ((s.node.active = !1),
                      window.ui.addScore(this.score, o.node),
                      soundMgr.playSound(soundMgr.selected))
                    : "鬼" == s.word
                    ? ((this.node.active = !1),
                      window.ui.fail({model: "PackMan", lv: window.packMan.level}),
                      soundMgr.playSound(soundMgr.wrong))
                    : "刺" == s.word || s.word);
        } else if ("电" == this.word || "刺" == this.word) {
            var e = t.getUserData(),
                a = e.other,
                n = (e.self, a.node.parent.parent.getComponent("PMWordBase"));
            n &&
                "我" == n.word &&
                (console.log(this.word + " collision enter" + n.word),
                (n.node.active = !1),
                window.ui.fail({model: "PackMan", lv: window.packMan.level}),
                soundMgr.playSound(soundMgr.wrong));
        }
    },
    onCollisionStay: function onCollisionStay() {},
    onCollisionExit: function onCollisionExit() {},
    triggerEvent: function triggerEvent(t, i) {
        this.triggerTime <= 0 &&
            ((this.triggerTime = this.triggerTimeMax), (this.trackTag = t), (this.trackTriggerPos = i));
    },
    updateTrackPos: function updateTrackPos() {
        if ("鬼" == this.word) {
            var t = window.packMan.getLogicPosByPixelPos(this.node.position),
                i = window.packMan.getLogicPosByPixelPos(this.trackTag.position),
                o = window.packMan.graphDiagonal.grid[t.y][t.x],
                e = window.packMan.graphDiagonal.grid[i.y][i.x],
                a = s.default.astar.search(window.packMan.graphDiagonal, o, e);
            a && a.length > 0 && ((this.trackRoadPoints = a), (this.trackRoadPointIndex = 0));
        }
    },
    updateAni: function updateAni() {
        if ("刺" == this.word || "电" == this.word) {
            var t = this.node.getComponent(cc.Animation);
            if (t && !t.isPlaying) {
                t.play();
                var i = window.packMan.getLogicPosByPixelPos(this.node.position);
                this.trackTriggerPos.x > i.x
                    ? (this.nodeAni.angle = 0)
                    : this.trackTriggerPos.x < i.x
                    ? (this.nodeAni.angle = 180)
                    : this.trackTriggerPos.y > i.y
                    ? (this.nodeAni.angle = 270)
                    : this.trackTriggerPos.y < i.y && (this.nodeAni.angle = 90),
                    (this.trackTag = null);
            }
        }
    }
});
